<template>
	<view class="content" style="padding-bottom: 50px;">
		<image :src="activityInfo.image" style="width:100%"></image>
		<view class="titleBar">
			<text class="title">{{activityInfo.className}}</text>
			<text class="count">已有{{activityInfo.number}}人报名</text>
		</view>

		<view class="courseInfo">
			<view>
				<text class="label">上课时间</text>
				<text class="colorRed" v-for="(item,index) in activityInfo.attendTime">{{item.week}} {{item.start_time}} — {{item.end_time}}</text>
			</view>
			<view><text class="label">上课地址</text><text>{{activityInfo.classAddress}}</text></view>
			<view><text class="label">课程时间</text><text class="colorRed">{{activityInfo.startTime|dateFormatYear }} — {{activityInfo.endTime|dateFormatYear }}</text></view>
			<view><text class="label">报名时间</text><text class="colorRed">{{activityInfo.applyStartTime }} — {{activityInfo.applyEndTime }}</text></view>
		</view>

		<view class="hr"></view>

		<view class="courseMain">
			<view class="h1">班级负责人</view>
			<view class="teachers">
				<view v-for='itm in activityInfo.activityTeacher' class="teachers-item" @click="goTeacherDetail(itm.teacherId)">
					<image :src="itm.teacherImage" mode="widthFix" class="avatar" />
					<view class="title">{{itm.teacherName}}</view>
					<view class="pos" v-if="itm.teacherRole==1">主教师</view>
					<view class="pos" v-if="itm.teacherRole==2">助教</view>
					<view class="pos" v-if="itm.teacherRole==3">班长</view>
				</view>
			</view>
		</view>

		<view class="tab">
			<view class="tab-item" :class="tabIndex==0?'active':''" @click="chgTab(0)">课程介绍 <view
					:class="tabIndex==0?'active':''"></view>
			</view>
			<view class="tab-item" :class="tabIndex==1?'active':''" @click="chgTab(1)">教材介绍 <view
					:class="tabIndex==1?'active':''"></view>
			</view>
			<view class="tab-item" :class="tabIndex==2?'active':''" @click="chgTab(2)">教学大纲 <view
					:class="tabIndex==2?'active':''"></view>
			</view>
			<view class="tab-item" :class="tabIndex==3?'active':''" @click="chgTab(3)">教学进度 <view
					:class="tabIndex==3?'active':''"></view>
			</view>
		</view>

		<view class="tab-content" v-if='tabIndex==0'>
			<rich-text :nodes="activityInfo.label"></rich-text>
			
			<!-- <view v-html="activityInfo.label"></view> -->
		</view>
		<view class="tab-content" v-if='tabIndex==1'>
			<view v-html="activityInfo.classMaterials.text"></view>
			<image :src="item2" mode="" v-for="(item2,index2) in activityInfo.classMaterials.images"></image>
			<image :src="item2" mode="" v-for="(item2,index2) in activityInfo.classMaterials.images"></image>
		</view>

		<view class="tab-content" v-if='tabIndex==2'>
			<view class="tab-content-item" v-for="(item3,index3) in activityInfo.classOutline">
				<view class="index">{{(index3+1)<10?'0'+(index3+1):(index3+1)}}</view>
				<view>
					{{item3}}
				</view>
			</view>
		</view>

		<view class="tab-content" v-if='tabIndex==3'>
			<view class="tab-content-item" :class="itm.status==-1?'hadEnd':''" v-for="(itm,index4) in activityInfo.classProgress"
				style="border-bottom: 1px solid #efefef;">
				<view class="index">{{(index4+1)<10?'0'+(index4+1):(index4+1)}}</view>
				<view>
					<text class="title">{{itm.title}}</text>
					<view>{{itm.outline}}</view>
					<view class="time">{{itm.start_time}} {{itm.flag==-1?'已结束':''}}</view>
				</view>
			</view>
		</view>


		<view class="footer" v-if="activityInfo.isApply==-1">
			<view class="point">课程价格：<text class="colorRed">￥{{activityInfo.price}}</text></view>
			<view class="applyBtn" @tap="open()">立即报名</view>
		</view>
		<view class="content">
			<Modal v-model="show" :text='confirmText' @cancel='cancel' @confirm='confirm' />
		</view>
		<view v-if="activityInfo.isApply==1" class="footer animated fadeInUp">
			<button class="btn2 box d-ai-c d-jc-c" @tap='signModal(activityInfo)'
				:class="activityInfo.isSign==0||activityInfo.isSign==1?'btn2-active':''">
				<text v-if="activityInfo.isSign==0">立即签到</text>
				<text v-if="activityInfo.isSign==3">已签到</text>
				<text v-if="activityInfo.isSign==1">立即签退</text>
				<text v-if="activityInfo.isSign==4">已签退</text>
				<text v-if="activityInfo.isSign==2">今天暂无此课程</text>
			</button>
		</view>
		<wybpopup ref="popup" type='bottom'>
			<view class="popup">
				<view class="pop-t">
					<image class="pop_images1" src="../../static/guanbi@2x.png" mode="" @tap="close()"></image>
					<view>选择支付方式</view>
				</view>
				<view class="pop-c">
					<text>￥</text>
					<text>{{activityInfo.price}}</text>
				</view>
				<radio-group @change="radioChange">
					<label class="uni-lab" v-for="(item, index) in items" :key="item.value">
						<view class="">
							<image v-if="item.value==='1'" class="pop_images2" src="../../static/jfzf@2x.png" mode=""></image>
							<image v-if="item.value==='2'" class="pop_images2" src="../../static/wxzf@2x.png" mode=""></image>
							<view class="pop-name">
								<text>{{item.name}}</text>
								<text v-if="item.value==='1'">(剩余{{accumulatePoints}}积分)</text>
							</view>
						</view>
						<view>
							<radio :value="item.value" :checked="index === current" />
						</view>
					</label>
				</radio-group>
				<view class="payment" type="default" @tap='createOrders'>
					立即支付
				</view>
		
		
			</view>
		
		</wybpopup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import Modal from '@/components/x-modal/x-modal'
	import wybpopup from '@/components/wyb-popup/wyb-popup.vue'
	export default {
		computed: mapState(['loginState', 'userInfo']),
		components: {
			wybpopup,
			Modal
		},
		data() {
			return {
				cimg: 'https://xb-street.oss-cn-shanghai.aliyuncs.com/yl-school/static/tmp/1.png',
				title: '书法班',
				count: 21,
				point: 150,
				classTime: '星期五 9:00-10:30',
				classAddress: '梅巷社区',
				classTime1: '2021-09-06 —— 2020-01-09',
				teachers: [{
						avatar: 'https://xb-street.oss-cn-shanghai.aliyuncs.com/yl-school/static/tmp/a1.png',
						title: '陈松',
						pos: '授课老师',
						id: 0
					},
					{
						avatar: 'https://xb-street.oss-cn-shanghai.aliyuncs.com/yl-school/static/tmp/a2.png',
						title: '王先生',
						pos: '班长',
						id: 1
					},
					{
						avatar: 'https://xb-street.oss-cn-shanghai.aliyuncs.com/yl-school/static/tmp/a3.png',
						title: '曹阿姨',
						pos: '副班长',
						id: 2
					},
				],
				tabIndex: 0,
				tabContent0: '书法是中国及深受中国文化影响过的周边国家和地区特有的一种文字美的艺术表现形式。包括汉字书法、蒙古文书法、阿拉伯书法和英文书法等。其“中国书法”，是中国汉字特有的一种传统艺术。<br>从广义讲，书法是指文字符号的书写法则。换言之，书法是指按照文字特点及其含义，以其书体笔法、结构和章法书写，使之成为富有美感的艺术作品。汉字书法为汉族独创的表现艺术，被誉为：无言的诗，无行的舞；无图的画，无声的乐等。<br>2018年12月，教育部办公厅关于公布绍兴文理学院为书法中华优秀传统文化传承基地 [1]  。2021年4月29日，中华人民共和国教育部经研究，决定成立教育部中国书法教育指导委员会 [6]  。',
				tabContent1: '<p>所用教材：毛笔、宣纸、.....等</p>',
				tabContent2: [{
						'title': '书法概论',
						'desc': '了解书法的含义、历史和性质'
					},
					{
						'title': '颜体楷书',
						'desc': '了解颜体楷书的基本特征'
					},
					{
						'title': '笔法掌握',
						'desc': '掌握"点"、"横"基本笔画的书写技巧，培养细致的观察、模仿、创造能力，锻炼毅力和耐性'
					},
				],
				tabContent3: [{
						'title': '书法概论',
						'desc': '了解书法的含义、历史和性质',
						time: '2021-09-10 9:00-10:30',
						status: '-1'
					},
					{
						'title': '颜体楷书',
						'desc': '了解颜体楷书的基本特征',
						time: '2021-09-17 9:00-10:30',
						status: '-1'
					},
					{
						'title': '笔法',
						'desc': '掌握"点"、"横"基本笔画的书写',
						time: '2021-09-24 9:00-10:30',
						status: '0'
					},
				],
				id: '',
				activityInfo: '',
				items: [{
						value: '1',
						name: '积分支付'
					},
					{
						value: '2',
						name: '微信支付',
						checked: 'true'
					}
				
				],
				current: 0,
				accumulatePoints:'',
				confirmText: '',
				locations: [],
				signModalType: 0, // 1 签到弹窗 2 报名弹窗确认
				show:false,
				num:'',
				accessId:''
			}
		},
		onLoad(parameter) {
			this.accessId = uni.getStorageSync('accessId')
			this.id = parameter.id
			this.getActivityInfo()
			this.init()

		},
		onShow() {
			this.getUserInfo()
			
		},
		methods: {
			...mapMutations(['logout', 'setUserInfo']),
			// 获取用户信息
			getUserInfo() {
				this.$api.getUserInfo().then((res) => {
					let userInfo = res.data.dataMap
					// this.getUserIn = res.data.dataMap
					console.log(userInfo)
					if (userInfo != null) {
						this.setUserInfo(userInfo)
						this.accumulatePoints = this.userInfo.userInfo.accumulatePoints
					}
			
				})
			
			},
			getActivityInfo() {
				this.$api.getClassOrActivityInfo({
					id: this.id
				}).then((res) => {
					this.activityInfo = res.data.dataMap
					this.activityInfo.startTime = this.renderTime(this.activityInfo.startTime)
					this.activityInfo.endTime = this.renderTime(this.activityInfo.endTime)
					this.num = this.activityInfo.price
					console.log(this.activityInfo)
					
				})
			},
			// 时间转换
			renderTime(date) {
				var dateee = new Date(date).toJSON();
				return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/,
					'')
			},
			open() {
				if (!this.loginState) {
					uni.navigateTo({
						url: '../mine/auth'
					})
					return
				}else{
					console.log(this.$refs)
					this.$refs.popup.show()
				}
				
			},
			close() {
				// this.$refs.popup.close()
				this.$refs.popup.hide()
			},
			radioChange: function(evt) {
				for (let i = 0; i < this.items.length; i++) {
					if (this.items[i].value === evt.detail.value) {
						this.current = i;
						break;
					}
				}
			},
			chgTab(index) {
				this.tabIndex = index;
			},
			goTeacherDetail(id) {
				uni.navigateTo({
					url: "teacher_detail?id=" + id
				})
			},
			//创建订单
			createOrders() {
				
				let type=1
				if(this.current===0){
					type=1
				}
				if(this.current===1){
					type=2
				}
				let parameter = {
					classOrActivityId: this.id
				}
				console.log(type)
				let _this = this
				this.$api.createOrders(parameter).then(res => {
					if (res.data.success) {
						if(type==1){
							_this.applyActivity(res.data.dataMap.ordersNumber,type)
						}else if(type==2){
							_this.applyActivity1(res.data.dataMap.ordersNumber,type)
						}
						
					} else if(res.data.code==20045){
						uni.showToast({
							title: res.data.message,
							duration: 1500,
							icon: "none"
						});
						setTimeout(function(){
							uni.navigateTo({
								url:'../mine/cert',
								animationType: 'pop-in',
								animationDuration: 200
							})
						},1000)
						this.close()
					}else {
						_this.close()
						uni.showToast({
							title: res.data.message,
							duration: 1500,
							icon: "none"
						});
					}
				})
			
			},
			applyActivity1(ordersNumber,type) {
				let _this = this
				this.$api.applyActivity({
					ordersNumber: ordersNumber,
					payType:'JSAPI',
					type:2,
					accessId:_this.accessId
				}).then((res) => {
						console.log("11111111111111111111")
					if(res.data.success){
						console.log(res.data)
					 if(type==2){	
						 console.log("22222222222222222")
							uni.requestPayment({
								appId:res.data.dataMap.appid,
							    provider: 'wxpay',
							    timeStamp:res.data.dataMap.timeStamp,
							    nonceStr: res.data.dataMap.nonceStr,
							    package: res.data.dataMap.package,
							    signType: 'MD5',
							    paySign: res.data.dataMap.paySign,
							    success: function (res) {
							        console.log('success:' + JSON.stringify(res));
									uni.showToast({
										title: '支付成功',
										duration: 500,
										icon: "none"
									});
									uni.navigateBack({
										delta:1,
										animationDuration:1000
									})
									
							    },
							    fail: function (err) {
							       uni.showToast({
							       	title: '支付失败',
							       	duration: 500,
							       	icon: "none"
							       });
							    }
							});
			
					}
					}else{
						_this.close()
						uni.showToast({
							title: res.data.message,
							duration: 2000,
							icon: "none"
						});
					}
			
				})
			},
			applyActivity(ordersNumber,type) {
				let _this = this
				this.$api.applyActivity({
					ordersNumber: ordersNumber,
					type:type
				}).then((res) => {
					if(res.data.success){
					if(type==1){ //余额支付
						if (res.data.success) {
							_this.activityInfo.isApply = 1
							uni.showToast({
								title: '报名成功！',
								duration: 1500,
								icon: "none"
							});
							this.getActivityInfo()
							this.close()
							this.$emit('carData',2)
							uni.navigateBack({
								delta:1
							})
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 2500,
								icon: "none"
							});
							if (res.data.code == 20020) {
								setTimeout(function() {
									uni.navigateTo({
										url: '../mine/recharge'
									})
								}, 1000);
							}
						
						}
					}
					}else{
						this.close()
						uni.showToast({
							title: res.data.message,
							duration: 2500,
							icon: "none"
						});
					}
				
			
				})
			},
			cancel(e) {
				uni.showToast({
					title: '已取消',
					duration: 1500,
					icon: "none"
				});
			},
			confirm(e) {
				if (this.signModalType == 1) {
					this.signIn()
				} else if (this.signModalType == 2) {
					this.createOrders()
					// this.applyActivity()
				}
			},
			signModal(activityInfo) {
				if (activityInfo.isSign == 2) {
					uni.showToast({
						title: '今天暂无此课程',
						duration: 1500,
						icon: "none"
					});
					return
				}
				if (activityInfo.isSign == 3) {
					uni.showToast({
						title: '已签到',
						duration: 1500,
						icon: "none"
					});
					return
				}
				if (activityInfo.isSign == 4) {
					uni.showToast({
						title: '已签退',
						duration: 1500,
						icon: "none"
					});
					return
				}
				if (this.activityInfo.isSign == 0) {
					this.confirmText = '您确定要签到吗?'
				} else if (this.activityInfo.isSign == 1) {
					this.confirmText = '您确定要签退吗?'
				}
				this.signModalType = 1 //签到弹窗
				this.show = true
			},
			init(){
				console.log('1111111111')
				uni.getLocation({
					type: 'wgs84',
					success: (res)=> {
						this.locations.length = 0
						this.locations.push(res.longitude, res.latitude)
						console.log(this.locations)
						console.log('当前位置的经度：' + res.longitude);
						console.log('当前位置的纬度：' + res.latitude);
						
					},
					fail() {
						uni.showToast({
							title: "请检查位置是否开启",
							duration: 1500,
							icon: "none"
						});
						console.log("失败了")
							
							
					}
				});
			},
			signIn() {
				let _this = this
				_this.$api.signUpOrOut({
					classOrActivityId: _this.id,
					locations: JSON.stringify(_this.locations)
				}).then((res) => {
					if (res.data.success) {
						let message = ''
						let isSign = -1
						if (_this.activityInfo.isSign == 0) {
							message = '签到成功'
							isSign = 3
						}
						if (_this.activityInfo.isSign == 1) {
							message = '签退成功'
							isSign = 4
						}
						_this.activityInfo.isSign = isSign
						uni.showToast({
							title: message,
							duration: 1500,
							icon: "none"
						});
					} else {
						console.log('签到签到')
						uni.showToast({
							title: res.data.message,
							duration: 1500,
							icon: "none"
						});
					}
					// uni.hideLoading();
							
				})
					
				// } else {
				// 	uni.showToast({
				// 		title: "请检查位置是否开启",
				// 		duration: 1000,
				// 		icon: "none"
				// 	});
				// 	return;
				// }
			
			},
			applyActivityModal() {
				this.signModalType = 2 //报名提示弹窗
				this.confirmText = '您确定要报名吗?'
				this.show = true
			},
		}
	}
</script>

<style lang="scss">
	.payment{
		width: 670rpx;
		height: 88rpx;
		background: #FF5551;
		border-radius: 44rpx;
		font-size: 32rpx;
		font-weight: 500;
		color: #FFFFFF;
		text-align: center;
		line-height: 88rpx;
		margin: 0 auto;
				margin-top: 50rpx;
	}
	.pop-name>text:nth-child(1){
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		margin-left: 13rpx;
	}
	.pop-name>text:nth-child(2){
		font-size: 32rpx;
		font-weight: 500;
		color: #999999;
		line-height: 120rpx;
	}
	.uni-lab>view:nth-child(1){
		margin-left: 20rpx;
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}
	.uni-lab{
		width: 690rpx;
		height: 100rpx;
		margin: 0rpx auto;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20rpx;
	}
	.pop_images2{
		width: 52rpx;
		height: 52rpx;
	}
	.pop-c{
		text-align: center;
		margin-bottom: 60rpx;
	}
	.pop-c>text:nth-child(1){
		font-size: 40rpx;
		font-weight: 800;
		color: #333333;
	}
	.pop-c>text:nth-child(2){
		font-size: 80rpx;
		font-weight: 800;
		color: #333333;
	}
	.pop-t{
		display: flex;
		align-items: center;
		text-align: center;
		margin-top: 40rpx;
	}
	.pop-t>view{
		font-size: 30rpx;
		font-weight: bold;
		color: #333333;
		margin-left: 210rpx;
	}
	.pop_images1{
		width: 48rpx;
		height: 48rpx;
		margin-left: 40rpx;
	}
	.popup {
		width: 750rpx;
		height: 785rpx;
		background: #FFFFFF;
		border-radius: 60rpx 60rpx 0rpx 0rpx;
		overflow: hidden;
	}
	.courseInfo {
		display: flex;
		flex-direction: column;
		line-height: 60rpx;
		padding: 5px 20rpx;

		.label {
			color: gray;
			margin-right: 30rpx;
		}

	}

	.titleBar {
		margin: 10px;
		border-bottom: 1px solid #efefef;
		padding: 5px 0;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.title {
			font-size: 36rpx;
			font-weight: bold;
		}

		.count {
			font-size: 28rpx;
			color: gray;
		}
	}

	.teachers {
		display: flex;

		.teachers-item {
			width: 24%;
			border-radius: 10px;
			box-shadow: 1px 2px 10px #eeeeee;
			display: flex;
			text-align: center;
			padding: 30rpx 0;
			margin: 20rpx 1%;
			flex-direction: column;

			.avatar {
				border-radius: 50%;
				width: 50%;
				height: 90rpx;
				margin: auto
			}

			.title {
				font-size: 32rpx;
			}

			.pos {
				color: gray;
			}
		}
	}

	.tab {
		display: flex;
		justify-content: space-around;
		margin-top: 20px;
		border-bottom: 1px solid #e7e7e7;

		.active {
			font-weight: bold;
		}

		.tab-item {
			border-bottom: 5px solid white;

			.active {
				background: linear-gradient(90deg, rgb(255, 228, 106), rgb(249, 155, 68));
				border-radius: 20rpx;
				margin: auto;
				margin-top: 5px;
				height: 5px;
				width: 60%;
			}
		}
	}

	.tab-content {
		padding: 10px;
		line-height: 52rpx;
		image{
			width: 180rpx;
			height: 120rpx;
			margin-right: 10rpx;
			border-radius: 10rpx;
		}

		.tab-content-item {
			display: flex;
			margin: 5px 0;
			padding: 5px 0;

			.index {
				padding: 0 10px;
			}

			.title {
				font-weight: bold;
			}

			.time {
				color: gray;
			}

		}

		.hadEnd {
			color: gray;

			.title {
				color: gray;
			}
		}
	}

	.footer {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 95%;
		background-color: white;
		border-top: 1px solid #efefef;
		padding: 5px 10px 20px 10px;
		display: flex;
		align-items: center;
		height: 50px;
		justify-content: space-around;

		.applyBtn {
			margin-left: 30px;
			flex: 1;
			text-align: center;
			background: rgb(254, 85, 51);
			border-radius: 20px;
			color: white;
			line-height: 40px;
			width: 100%;
		}
		.btn2 {
			width: 700rpx;
			height: 80rpx;
			color: #fff;
			background-color: #9c9896;
			line-height: 80rpx;
			font-size: 32rpx;
			text-align: center;
			margin: 0 auto;
		}
		
		.btn2-active {
			background-color: #FF5551 !important;
			line-height: 80rpx;
			font-size: 32rpx;
			text-align: center;
			margin: 0 auto;
		}
	}
</style>
